﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CRM_SharePoint_Transports
{
    public partial class SPPropertyForm : Form
    {
        public SPPropertyForm (SPProperty property, string[] tokens)
        {
            InitializeComponent();

            if (tokens != null)
            {
                foreach (string t in tokens)
                {
                    this.comboBox2.Items.Add("%" + t + "%");
                }
            }

            m_comboMap = new List<int>();

            foreach (string s in System.Enum.GetNames(typeof(SPCopy.FieldType)))
            {
                this.comboBox1.Items.Add(s);
                m_comboMap.Add ((int)System.Enum.Parse(typeof(SPCopy.FieldType), s));
            }

            if (property != null)
            {
                this.textBox1.Text = property.DisplayName ?? "";
                this.textBox2.Text = property.InternalName ?? "";

                for (int i = 0; i < m_comboMap.Count; i++)
                {
                    if (m_comboMap[i] == (int)property.Type)
                    {
                        this.comboBox1.SelectedIndex = i;
                        break;
                    }
                }

                this.comboBox2.Text = property.Binding;
                this.ResultProperty = property;
            }
            else
            {
                this.comboBox1.SelectedIndex = this.comboBox1.Items.IndexOf("Text");
            }
        }

        List<int> m_comboMap;
        public SPProperty ResultProperty = new SPProperty();

        /// <summary>
        /// Close handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// OK handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "" || this.textBox2.Text == "" || this.comboBox1.SelectedIndex < 0)
            {
                MessageBox.Show("Error - missing data");
                return;
            }

            this.ResultProperty.DisplayName = this.textBox1.Text;
            this.ResultProperty.InternalName = this.textBox2.Text;
            this.ResultProperty.Type = (SPCopy.FieldType)m_comboMap[this.comboBox1.SelectedIndex];
            this.ResultProperty.Binding = this.comboBox2.Text;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        string m_name1, m_name2;

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (m_name1 == m_name2)
                textBox2.Text = textBox1.Text;

            m_name1 = textBox1.Text;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            m_name2 = textBox2.Text;
        }
    }
}
